/*
 * Copyright (C) 2009 CityLights Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.schizoidman.android.citylights;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class ProjectWidgetProvider extends AppWidgetProvider {
	
	// Android SDK 1.5 Bug workaround
	@Override 
	public void onReceive(Context context, Intent intent) { 
		Log.i("CityLights", "onRecieve(" + intent + ") ");
		final String action = intent.getAction(); 
		final Bundle extras = intent.getExtras();
		if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) { 
			final int appWidgetId = extras.getInt 
				(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); 
				if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) { 
					this.onDeleted(context, new int[] { appWidgetId }); 
				} 
		}
		else { 
			super.onReceive(context, intent); 
		}
	} 	
	
	/* (non-Javadoc)
	 * @see android.appwidget.AppWidgetProvider#onUpdate(android.content.Context, android.appwidget.AppWidgetManager, int[])
	 */
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		Log.i("CityLights", "onUpdate().... doing nothing");
//		
//		final int N = appWidgetIds.length;
//        for (int i=0; i<N; i++) {
//            int appWidgetId = appWidgetIds[i];
//    		Log.i("CityLights", "... widget: " + appWidgetId);
//            Intent intent = WidgetRegistry.getRegistry(context).loadWidgetPrefs(appWidgetId);
//            if (intent != null) {
//            	intent.setClass(context, ProjectWidgetUpdateService.class);
//                context.startService(intent);
//            }
//        }
//        
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}
	
	/* (non-Javadoc)
	 * @see android.appwidget.AppWidgetProvider#onDeleted(android.content.Context, int[])
	 */
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		Log.i("CityLights", "onDelete()....");

		final int N = appWidgetIds.length;
        for (int i=0; i<N; i++) {
        	WidgetRegistry.getRegistry(context).eraseWidgetPrefs(appWidgetIds[i]);
        }
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onEnabled(Context context) {
	    Log.d("CityLights", "onEnabled(" + context.getPackageName() + ")");
	    // We should engage alarmservice here in case we are upon re-boot
	    MonitorWidgetBroadcastReceiver.engageAlarmService(context);
	}

	@Override
	public void onDisabled(Context context) {
	    Log.d("CityLights", "onDisabled(" + context.getPackageName() + ")");
	    //TODO: This is workaround. Need to find/fix the problem of shadow widget entries
//    	WidgetRegistry.getRegistry(context).purgeWidgetPrefs();
	}

}
